<template>
    <div class="app-container">

        <!-- 添加修改弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="dialogVisible"
                width="500px"
                center
                append-to-body
        >
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="科室名称" prop="deptName">
                    <el-input v-model="form.deptName" placeholder="请输入科室名称" clearable size="small" />
                </el-form-item>
                <el-form-item label="科室编码" prop="deptNumber">
                    <el-input v-model="form.deptNumber" placeholder="请输入科室编码" clearable size="small" />
                </el-form-item>
                <el-form-item label="挂号开始编号" prop="regNumber">
                    <el-input-number v-model="form.regNumber" placeholder="请输入挂号开始编号" clearable size="small" />
                </el-form-item>
                <el-form-item label="负责人" prop="deptLeader">
                    <el-input v-model="form.deptLeader" placeholder="请输入负责人" clearable size="small" />
                </el-form-item>
                <el-form-item label="电话" prop="leaderPhone">
                    <el-input v-model="form.leaderPhone" placeholder="请输入负责人电话" clearable size="small" />
                </el-form-item>
                <el-form-item label="排序码" prop="orderNum">
                    <el-input-number v-model="form.orderNum" clearable size="small" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio
                                v-for="dict in statusOptions"
                                :key="dict.dictValue"
                                :label="dict.dictValue"
                                :value="dict.dictValue"
                        >{{ dict.dictLabel }}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
        </el-dialog>
        <!-- 添加修改弹出层结束 -->

        <!-- 查询条件开始 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="科室名称" prop="deptName">
                <el-input
                        v-model="queryParams.deptName"
                        placeholder="请输入科室名称"
                        clearable
                        size="small"
                        style="width:140px"
                />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                        v-model="queryParams.status"
                        placeholder="可用状态"
                        clearable
                        size="small"
                        style="width:140px"
                >
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker
                        v-model="queryParams.dateRange"
                        size="small"
                        style="width:210px"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholde="开始日期"
                        end-placeholde="结束日期"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" circle @click="queryTable"></el-button>
                <el-button type="warning" icon="el-icon-refresh" circle @click="refreshDept"></el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->

        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" :disabled="multiple" size="mini" @click="mulDel">批量删除</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->

        <!-- 数据表格开始 -->
        <el-table  border :ref="multipleTable" :data="deptTableList" fit @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="科室ID" align="center" prop="deptId" sortable width="100"/>
            <el-table-column label="科室名称" align="center" prop="deptName" />
            <el-table-column label="科室编码" align="center" prop="deptNumber" />
            <el-table-column label="当前挂号量" align="center" prop="regNumber" sortable/>
            <el-table-column label="排序码" align="center" prop="orderNum" />
            <el-table-column label="负责人" align="center" prop="deptLeader" />
            <el-table-column label="电话" align="center" prop="leaderPhone" />
            <el-table-column label="状态" align="center" prop="status"  :formatter="statusFormat" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="170"/>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" size="mini" @click="updDept(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" size="mini" @click="delDept(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->

        <!-- 分页控件开始 -->
        <el-pagination
                v-show="page.total>0"
                :current-page="page.current"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <!-- 分页控件结束 -->
    </div>
</template>

<script>
    import qs from 'qs';
    export default {
        name: "Dept",
        data(){
            return{
                //模糊查询的条件
                queryParams:{},
                // 非多个禁用
                multiple: true,
                //表格数据
                deptTableList:[],
                //多选框选中的数据
                multipleTable:[],
                //系统状态
                statusOptions:[],
                //分页数据
                page:{
                    current:1,
                    size:5,
                    total:0
                },
                //添加/修改表单的控值
                dialogVisible:false,
                //表单标题
                title:'',
                //表单绑定的对象
                form:{},
                //表单绑定的规则
                rules:{
                    deptName: [
                        { required: true, message: '科室名称不能为空', trigger: 'blur' }
                    ],
                    deptNumber: [
                        { required: true, message: '科室编码不能为空', trigger: 'blur' }
                    ]
                },
            }
        },
        created() {
            this.initStatus();     //加载状态条件模糊查询
            this.initDept();       //加载表格中科室信息
        },
        methods:{
            initDept(){  //查询表格中数据
              this.$http.post("/system/api/dept/selectAll/"+this.page.current+"/"+this.page.size,this.queryParams).then(result=>{
                    this.deptTableList = result.data.data.records;
                    this.page.total = result.data.data.total;
              })
            },
            /* 表格外部按钮事件 */
            queryTable(){    //模糊查询按钮触发
                this.page.current=1;
                this.initDept();
            },
            refreshDept(){  //重置模糊查询按钮触发
                this.queryParams={};
                this.initDept();
            },
            handleAdd(){  //添加按钮触发
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                //弹出添加/修改表单
                this.dialogVisible=true;
                this.reset()   //重置表单
                this.title = '添加科室信息'
            },
            mulDel(){  //批量删除按钮触发
                this.$confirm('此操作将永久删除以下被选中科室, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let deptIds = [];
                    for (let i=0;i<this.multipleSelection.length;i++){
                        deptIds.push(this.multipleSelection[i].deptId)
                    }
                    this.$http.post("/system/api/dept/muldelDept?deptIds="+deptIds).then(result=>{
                        if (result.data.data.length>0){
                            this.$message.error("科室id为"+result.data.data+"的科室下面还存在用户，不能进行删除！");
                        }else {
                            this.$message.success(result.data.msg);
                        }
                        this.initDept();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            /* 表单事件 */
            handleSubmit(){  // 添加/修改表单确定按钮触发
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.$http.post("/system/api/dept/saveOrUpdateDept",this.form).then(result=>{
                            if (result.data.data){
                                this.$message({
                                    showClose: true,
                                    message: result.data.msg,
                                    type: 'success'
                                });
                            }else {
                                this.$message({
                                    showClose: true,
                                    message: '操作失败',
                                    type: 'error'
                                });
                            }

                            // 关闭弹出对话框
                            this.dialogVisible = false;
                            // 重置表单
                            this.reset();
                            this.title='';
                            //表格回溯到第一页
                            this.page.current=1;
                            // 刷新表格数据
                            this.initDept();
                        })
                    }
                })
            },
            cancel(){     //添加修改表单取消按钮触发
                this.dialogVisible=false;
                this.title = '';
            },

            /* 表格内部点击事件 */
            handleSelectionChange(val){  //表格选择事件触发，将数据存放到multipleSelection中
                this.multipleSelection = val;
                //没有行被选中的时候，将批量删除按钮禁用
                this.multiple = !val.length
            },
            updDept(row){  //表格中修改按钮触发
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                this.title='修改科室信息';
                //显示弹出对话框
                this.dialogVisible = true;
                this.form.deptId = row.deptId;
                //采取深克隆的形式
                this.form = JSON.parse(JSON.stringify(row));
            },
            delDept(row){  //表格中删除按钮触发
                this.$confirm('此操作将永久删除该科室, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("/system/api/dept/delDept/"+row.deptId).then(result=>{
                        if (result.data.data!=null){
                            this.$message.error("科室id为"+result.data.data+"的科室下面还存在用户，不能进行删除！")
                        }else {
                            this.$message.success(result.data.msg)
                        }
                        this.page.current=1;
                        this.initDept();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },


            /*初始化数据*/
            initStatus(){   //初始化状态字典
                this.$http.get("/system/api/dict/data/findByType/sys_normal_disable").then(result=>{
                    this.statusOptions = result.data.data;
                })
            },
            statusFormat(row){  //状态字典解析
                let v = row.status;
                return this.formatDict2(this.statusOptions,v);
            },
            // 分页pageSize变化时触发
            handleSizeChange(val) {
                this.page.size = val
                // 重新查询
                this.initDept()
            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleCurrentChange(val) {
                this.page.current = val
                // 重新查询
                this.initDept()
            },
            reset(){  //重置表单数据
                //取消之后清空表单数据
                this.form = {
                    deptId: undefined,
                    deptName: undefined,
                    deptLeader: undefined,
                    deptNumber: undefined,
                    orderNum: 0,
                    regNumber: 0,
                    status: '0'
                }
            }
        }
    }
</script>

<style scoped>

</style>
